package priv.yiyuan.example.design.flyweight;

import java.awt.*;
import java.util.Objects;

public class CharacterFormat {

    private final String fontName;

    private final int fontSize;

    private final Color color;

    private final boolean bold;

    private final boolean italic;

    public CharacterFormat(String fontName, int fontSize, Color color, boolean bold, boolean italic) {
        this.fontName = fontName;
        this.fontSize = fontSize;
        this.color = color;
        this.bold = bold;
        this.italic = italic;
        System.out.println("创建新的字符格式享元: [" + toString() + "]");
    }

    public void render(char character, int x, int y) {
        System.out.println(String.format("渲染字符 \'%c\' 在 (%d, %d) 使用格式: %s%n", character, x, y, toString()));
    }

    @Override
    public String toString() {
        return "Font: " + fontName + ", Size: " + fontSize + ", Color: " + color.getRed() + ", Bold: " + bold + ", Italic: " + italic;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        CharacterFormat that = (CharacterFormat) o;
        return fontSize == that.fontSize &&
                bold == that.bold &&
                italic == that.italic &&
                Objects.equals(fontName, that.fontName) &&
                Objects.equals(color, that.color);
    }

    @Override
    public int hashCode() {
        int result = fontName.hashCode();
        result = 31 * result + fontSize;
        result = 31 * result + color.hashCode();
        result = 31 * result + (bold ? 1 : 0);
        result = 31 * result + (italic ? 1 : 0);
        return result;
    }
}
